<!DOCTYPE html>
<title>HTMLCollection liveness during item removal</title>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<!-- Regression test for https://github.com/jsdom/jsdom/issues/1517 -->

<div id="container1">
  <div class="test">1</div>
  <div class="test">2</div>
  <div class="test">3</div>
  <div class="test">4</div>
</div>

<div id="container2">
  <div class="test2">1</div>
  <div class="test2">2</div>
  <div class="test2">3</div>
  <div class="test2">4</div>
</div>

<script>
"use strict";

test(() => {
  const container = document.getElementById("container1");
  const collection = container.getElementsByClassName("test");
  const processed = [];

  while (collection.length > 0) {
    const item = collection.item(0);
    assert_not_equals(item, null, "item(0) should not be null when length > 0");
    processed.push(item.textContent);
    item.parentNode.removeChild(item);
  }

  assert_array_equals(processed, ["1", "2", "3", "4"], "All items should be processed in order");
}, "getElementsByClassName collection should update live when removing item(0)");

test(() => {
  const collection = document.body.getElementsByClassName("test2"); // Query from body
  const processed = [];

  while (collection.length > 0) {
    const item = collection.item(0);
    assert_not_equals(item, null, "item(0) should not be null when length > 0");
    processed.push(item.textContent);
    item.parentNode.removeChild(item);
  }

  assert_array_equals(processed, ["1", "2", "3", "4"], "All items should be processed in order");
}, "document.body.getElementsByClassName collection should update live when removing item(0)");

</script>
